<template>
  <div class="accountManagement">
    <el-card class="box-card">
      <el-form>
        <el-form-item>
          <el-input
            v-model="queryData.name"
            placeholder="员工姓名"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="queryData.state" placeholder="请选择" clearable>
            <el-option label="启用" value="0"></el-option>
            <el-option label="禁用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handlerSeach">查询</el-button>
          <el-button type="primary" @click="handlerAddBtn">新增</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card">
      <el-table
        v-loading="loading"
        :data="accountList"
        border
        style="width: 100%"
      >
        <el-table-column align="center" prop="name" label="员工姓名">
        </el-table-column>
        <el-table-column prop="rname" align="center" label="所属角色">
        </el-table-column>
        <el-table-column prop="state" align="center" label="状态">
          <template slot-scope="scope">
            <div
              style="font-weight: 600"
              :class="scope.row.state == 0 ? 'green' : ''"
            >
              {{ scope.row.state == 0 ? "启用" : "禁用" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="editAccounts(scope.$index, scope.row)"
              >修改</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="handlerDelete(scope.row)"
              >删除</el-button
            >
            <el-button
              type="text"
              v-show="scope.row.state == 1"
              @click="handlerEnable(scope.row)"
              size="small"
              >启用</el-button
            >
            <el-button
              type="text"
              v-show="scope.row.state == 0"
              @click="handlerForbidden(scope.row)"
              size="small"
              >禁用</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="resetPossWord(scope.row)"
              >重置密码</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        :page.sync="queryData.pageNum"
        :limit.sync="queryData.pageSize"
        @pagination="getAccount"
      />
    </el-card>
    <!-- 新增弹窗 -->
    <AddAccount
      :addDialogVisible="addDialogVisible"
      @addAccountCancel="addAccountCancel"
      @addAccount="addAccount"
    />
    <!-- 修改弹窗 -->
    <EditAccount
      :editDialogVisible="editDialogVisible"
      :editForm="editForm"
      @editAccountCancel="editAccountCancel"
      @editAccount="editAccount"
    />
  </div>
</template>
<script>
// 引入接口文件
import {
  getAccount,
  addAccount,
  deletAccount,
  resetPwd,
  updateState,
  update
} from "../../api/account";

// 引入组件
import AddAccount from "./components/addAccount.vue";
import EditAccount from "./components/editAccount.vue";
import Pagination from "../../components/Pagination/index.vue";
export default {
  // 注册组件
  components: {
    AddAccount,
    EditAccount,
    Pagination,
  },
  data() {
    return {
      loading: true,  //判断加载图标
      queryData: {
        pageNum: 1, //当前显示多少数据
        pageSize: 8, // 当前页
        name: "", //角色名称
        state: "", //	账号状态 0：启用/1：禁用
      },
      total: 0, // 总条数
      addDialogVisible: false,  //打开新增弹窗判断条件
      editDialogVisible: false,  //打开修改弹窗判断条件
      editForm: {},   //储存修改的容器
      accountList: [],  //储存员工列表数据容器
    };
  },
  // 调用接口函数
  created() {
    this.getAccount();
  },
  methods: {
    // 请求数据
    getAccount() {
      getAccount(this.queryData).then((res) => {
        if (res.status === 200) {
          this.loading = false;  //关闭加载动画
          this.accountList = res.data.records;   //将请求数据传入存放容器
          this.total = res.data.total; //总条数
          this.queryData.pageNum = res.data.current; //当前页
          this.queryData.pageSize = res.data.size; //每页显示条数
        }
      });
    },
    // 打开新增弹窗按钮
    handlerAddBtn() {
      this.addDialogVisible = true;
    },
    // 取消新增按钮
    addAccountCancel(valFalse) {
      this.addDialogVisible = valFalse;
    },
    // 确认新增按钮
    addAccount(valList, valFalse) {
      addAccount(valList).then((res) => {
        if (res.status === 200) {
          this.getAccount();
          this.$message({
            message: "新增成功",
            type: "success",
          });
          this.addDialogVisible = valFalse;  //新增接口请求成功关闭弹窗
        }
      });
    },
    // 打开修改弹窗按钮
    editAccounts(index, row) {
      this.editDialogVisible = true;   
      this.editForm = Object.assign({}, row); //将数据传入修改弹窗页面
      this.editForm.index = index;  
    },
    // 取消修改按钮
    editAccountCancel(valFalse) {
      this.editDialogVisible = valFalse;
    },
    // 确认修改按钮
    editAccount(editForm, valFalse) {
      update(editForm).then(res=>{
        this.editDialogVisible = valFalse;
        this.getAccount();
      })
    },
    // 删除按钮
    handlerDelete(rows) {
      this.$confirm("确认是否删除该账号, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var userId = rows.id;
          deletAccount(userId).then((res) => {
            if (res.status === 200) {
              this.getAccount();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 重置密码
    resetPossWord(rows) {
      this.$confirm("重置密码,该账号可用初始密码登录,初始密码:123456", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          resetPwd({ id: rows.id }).then((res) => {});
          this.$message({
            type: "success",
            message: "重置成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消重置",
          });
        });
    },
    // 禁用按钮
    handlerForbidden(rows) {
      this.$confirm("禁用该角色,相关账号登录只能查看首页", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          updateState({ id: rows.id }).then((res) => {
            if (res.status === 200) {
              this.getAccount();
              this.$message({
                type: "success",
                message: "禁用成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消禁用",
          });
        });
    },
    // 启用按钮
    handlerEnable(rows) {
      this.$confirm("启用该角色,相关账号登录可查看相应资源菜单", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          updateState({ id: rows.id }).then((res) => {
            if (res.status === 200) {
              this.getAccount();
              this.$message({
                type: "success",
                message: "启用成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消启用",
          });
        });
    },
    // 查询按钮
    handlerSeach() {
      this.getAccount();
    },
  },
};
</script>
<style lang="scss" scoped>
.el-card {
  margin: 20px;
}
.el-input {
  width: 250px;
}
.el-form {
  display: flex;
  justify-content: space-evenly;
  .el-button {
    margin-right: 55px;
  }
}
.green {
  color: green;
}
</style>
